package zhieasy.cn.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.model.common.Role;
import zhieasy.cn.model.common.WechatUserRole;
import zhieasy.cn.persistence.mapper.common.RoleMapper;
import zhieasy.cn.persistence.mapper.common.WechatUserRoleMapper;
import zhieasy.cn.service.common.RoleService;

import java.util.List;

/**
 * @author DaHang
 * @date 2020/8/13 23:15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    WechatUserRoleMapper wechatUserRoleMapper;

    @Autowired
    RoleMapper roleMapper;

    @Override
    public WechatUserRole getRoleIdByWeChatUserOpenId(String openId) {

        return wechatUserRoleMapper.getRoleIdByOpenId(openId);
    }

    /**
     * 给这个人id，给这个人需要赋值的角色的id
     * @return
     */
    @Override
    public boolean assignRole(String openId, Long roleId) {
        WechatUserRole wechatUserRole = new WechatUserRole();
        wechatUserRole.setRoleId(roleId);
        wechatUserRole.setUserId(openId);
        //先删除该用户原来的角色
        QueryWrapper<WechatUserRole> query = new QueryWrapper<>();
        query.lambda().eq(WechatUserRole::getUserId,openId);
        wechatUserRoleMapper.delete(query);
        int insert = wechatUserRoleMapper.insert(wechatUserRole);
        if (insert == 1)
            return true;
        return false;
    }

    @Override
    public List<Role> getWechatUserRoleListByOpenId(String openId) {
        List<Role> roles = roleMapper.selectWechatUserRoleListByOpenId(openId);
        return roles;
    }

}
